package main

import "fmt"

func main() {
	//声明myMap1是一种map类型，key是string，value是string
	var myMap1 map[string]string
	if myMap1 == nil {
		fmt.Println("myMap1 是一个空map")
	}

	//在使用map之前，需要先使用make给map分配数据空间，如果空间不够了，会自动开辟新空间的
	myMap1 = make(map[string]string, 10)

	myMap1["one"] = "java"
	myMap1["two"] = "python"
	myMap1["three"] = "golang"
	myMap1["four"] = "c++"

	fmt.Println(myMap1) //乱序

	fmt.Println("------------------------------")
	var myMap2 map[int]string
	myMap2 = make(map[int]string)
	myMap2[1] = "java"
	myMap2[2] = "python"
	myMap2[3] = "golang"
	myMap2[4] = "c++"

	fmt.Println(myMap2)

	fmt.Println("------------------------------")
	myMap3 := map[string]string{
		"one":   "java",
		"two":   "python",
		"three": "golang",
		"four":  "c++",
	}
	fmt.Println(myMap3)

	fmt.Println("==========下面是map的基本使用方式==========")

	cityMap := make(map[string]string)

	//添加
	cityMap["China"] = "Beijing"
	cityMap["Japan"] = "Tokyo"
	cityMap["USA"] = "New York"

	//遍历
	for key, value := range cityMap {
		fmt.Println("key " + key)
		fmt.Println("value " + value)
	}

	//删除
	delete(cityMap, "China")

	//修改
	cityMap["USA"] = "DC"

	printMap(cityMap)

}

func printMap(cityMap map[string]string) {
	//遍历
	for key, value := range cityMap {
		fmt.Println("key " + key)
		fmt.Println("value " + value)
	}
}
